<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="项目代码">
          <el-input v-model="form.project_code" placeholder="项目代码" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onAdd">新增</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column
            prop="project_name"
            label="项目名称"
            align="center"
          />
          <el-table-column
            prop="project_code"
            label="项目代码"
            align="center"
          />
          <el-table-column prop="name" label="权限名称" align="center"  width="130" />
          <el-table-column prop="code" label="权限代码" align="center"  width="150"/>
          <el-table-column
            prop="method"
            label="操作方法"
            align="center"
            width="200"
            :formatter="formatterMethod"
          />
          <el-table-column prop="api_name" label="api名称" align="center"  width="200"/>
          <el-table-column
            prop="parent_code"
            label="父级权限代码"
            align="center"
            width="150"
          />
          <el-table-column prop="priority" label="排序数字" align="center" />
          <el-table-column label="操作" align="center" width="200" fixed="right">
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="handleEdite(scope.$index, scope.row)"
                >编辑</el-button
              >
              <el-button
                type="danger"
                @click="handleDelete(scope.$index, scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :methodList="methodList"
      :dialogFormVisible="dialogFormVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import DialogForm from "./components/dialogForm.vue";
import { exportToExcel } from "@/utils/utils";
import {
  permissionApi,
  methodApi,
  addPermissionApi,
  editePermissionApi,
  deletePermissionApi,
} from "@/api/system/system";

const loading = ref(false);
const tableData = ref([]);
const methodList = ref([]); //操作方法列表
const btnLoading = ref(false);

const type = ref(null);
const dialogFormVisible = ref(false);

// 编辑框的表单
const dialogFormData = {
  id: "",
  project_name: "",
  project_code: "",
  method: [],
  api_name: "",
  priority: "",
  menuList: [],
};
const dialogForm = reactive({ ...dialogFormData });

//查询表单
const formData = {
  project_code: "",
};
const form = reactive({ ...formData });

const pageable = reactive({
  pageNum: 1,
  pageSize: 20,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    project_code: form.project_code,
  };
};

//匹配操作方法对应的value值
//入参'add,delete,freeze,judge'

//出参 method['查询', '删除', '冻结', '判定']
const setMethodValue = (methodStr) => {
  let methodArr = methodStr.split(",");
  let arr = [];
  for (let i = 0; i < methodArr.length; i++) {
    for (let j = 0; j < methodList.value.length; j++) {
      if (methodArr[i] == methodList.value[j]["method"]) {
        arr.push(methodList.value[j]["name"]);
      }
    }
  }

  return arr;
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  try {
    const res = await permissionApi(param());
    loading.value = false;
    tableData.value = res.data.results;

    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
const formatterMethod = (row, column, cellValue, index) => {
  return setMethodValue(cellValue).join(",");
};
// 获取操作方法列表数据
const getMethodData = async () => {
  try {
    const res = await methodApi({});
    methodList.value = res.data;
  } catch (error) {}
};

// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
//新增
const onAdd = () => {
  dialogFormVisible.value = true;
  type.value = "add";
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await permissionApi(data);

  let head = {
    id: "ID",
    project_name: "项目名称",
    project_code: "项目代码",
    name: "权限名称",
    code: "权限代码",
    method: "操作方法",
    api_name: "api名称",
    parent_code: "父级权限代码",
    priority: "排序数字",
  };
  exportToExcel(res.data.results, head, "权限列表");
  
  
};
//编辑
const handleEdite = (index, row) => {
  console.log(row);
  dialogForm.id = row.id;
  dialogForm.project_name = row.project_name;
  dialogForm.project_code = row.project_code;
  dialogForm.method = row.method.split(",");
  dialogForm.api_name = row.api_name;
  dialogForm.priority = row.priority;
  dialogForm.menuList = [row.project_code, row.parent_code, row.code];
  dialogFormVisible.value = true;
  type.value = "edite";
};
// 删除

const handleDelete = (index, row) => {
  ElMessageBox.confirm(`您是否确认删除此权限菜单?`, "温馨提示", {
    type: "warning",
  }).then(async () => {
    const res = await deletePermissionApi(row.id, {});
    ElMessage.success(res.message);
    getTbaleData();
  });
};

//关闭弹窗
const closeDialog = () => {
  dialogFormVisible.value = false;
  Object.assign(dialogForm, dialogFormData);
  type.value = null;
};

// 取消
const cancel = () => {
  dialogFormVisible.value = false;
  Object.assign(dialogForm, dialogFormData);
  type.value = null;
};

// 提交
const submit = async (data) => {
  try {
    btnLoading.value = true;
    if (type.value == "add") {
      const res = await addPermissionApi(data);
      ElMessage({
        message: res.message,
        type: "success",
      });
    }
    // 编辑
    if (type.value == "edite") {
      const res = await editePermissionApi(data.id, data);
      ElMessage({
        message: res.message,
        type: "success",
      });
    }

    dialogFormVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    console.log(error);
    btnLoading.value = false;
    dialogFormVisible.value = false;
  }
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
  getMethodData();
});
</script>
